///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2009. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

//this._uri = new URI('gfx/images/jungle.png');
//this._uri = new URI('http://www.bandalismo.net/IMG/distant/gif/3408_1gif-040438.gif');
//this._uri = new URI('http://simoncpage.co.uk/blog/wp-content/uploads/2008/12/christmas-pixel-art-1.jpg');
//this._uri = new URI('http://s2.imgimg.de/uploads0/120790870511438bb0378png.png');
//this._uri = new URI('http://blog.turbomilk.com/images/pixel-office-large.gif');
//this._uri = new URI('http://www.dpk.com.ua/files/u4/pixel_city_part_2.png');
//this._uri = new URI('gfx/images/character/smb_mario_sheet.png');
//this._uri = new URI('gfx/images/character/mario_run.png');
//this._uri = new URI('http://www.gifandgif.eu/animated_gif/Arrows/Animated%20Gif%20Arrows%20(104).gif');
//this._uri = new URI('gfx/images/character/10-4.gif');

package
{

import com.bit101.components.Label;
import com.bit101.components.PushButton;

import flash.display.DisplayObject;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.system.System;

import ru.goodcore.core.GoodCore;
import ru.goodcore.logging.ILogger;
import ru.goodcore.logging.Log;
import ru.goodcore.logging.targets.BuiltInDebugTarget;
import ru.goodcore.net.URI;
import ru.goodcore.resource.ResourceBehavior;
import ru.goodcore.resource.ResourceDefinition;
import ru.goodcore.resource.ResourceManager;
import ru.goodcore.resource.ResourceVault;
import ru.goodcore.utils.ActivityListener;
import ru.goodcore.utils.NetworkingStatus;
import ru.goodcore.utils.getNetworkingStatus;

//import flash.display.Graphics;
//import flash.display.BitmapData;
/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="1024", height="768", frameRate="21", backgroundColor="#E0EEEE")]
//[SWF(width="740", height="600", frameRate="21", backgroundColor="#D91A6B")]

public class GoodCoreTest extends GoodCore {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function GoodCoreTest() {
		super();
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;
		
		var o:Object = new Object();
		o.foo = 'bar';
		trace(o.foo);
//trace()
super.loaderInfo.parameters.foo = 'bar';
trace(super.loaderInfo.parameters.foo);

		//Trace.setLevel(Trace.METHODS_AND_LINES_WITH_ARGS, Trace.LISTENER);
		//Trace.setListener(this.handler_trace);

		//var li:LoaderInfo = new LoaderInfo();
	}
	
	private function handler_trace(...args):void {
		trace('Trace: ' + args);
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 */
	private var _rdStorage:		Array;

	/**
	 * @private 
	 */
	private var _index:			uint = 0;

	/**
	 * @private 
	 */
	private var _yPos:			uint = 0;

	/**
	 * 
	 */	
	private var _logger:		ILogger;

	/**
	 * 
	 */	
	private var _label:			Label;

	/**
	 * 
	 */	
	private var _mc:			MovieClip;

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	public override function creationComplete():void {

		var activityListener:ActivityListener = new ActivityListener();
		activityListener.x = 5;
		activityListener.y = 5;
		super.stage.addChild(activityListener);

		var target:BuiltInDebugTarget = new BuiltInDebugTarget();

		target.formatter.includeMeasuredTime = true;

		Log.addTarget(target);
		this._logger = Log.getLogger(this);

		super.stage.addChild(target);
		
		this._label = new Label(super.stage, 193, 20);
		
		var button:PushButton = new PushButton(super.stage, 270, 10, 'play', this.handler_pressPlay);
		

		this._logger.info('Start session');

		GoodCore.keyboardManager.addKeyboardEventListener(KeyboardEvent.KEY_UP, target.open, false, int.MAX_VALUE, false, 68, 0, true);

		var uris:Array = [
		//	new URI('gfx/1084x766_oneframe.gif')
		//	new URI('gfx/dog2.gif')
		//	new URI('gfx/dog3.gif')
		//	new URI('gfx/135x94_oneframe.gif')
		//	new URI('gfx/beach_dancers.gif')
		//	new URI('gfx/dance_band.gif')
		//	new URI('gfx/dancing_lady.gif')
		//	new URI('gfx/dancing_lady_orig.gif')
		//	new URI('gfx/dog.gif')
		//	new URI('gfx/dog[transparent_interlaced].gif')
		//	new URI('gfx/doggy.gif') // STRANGE FRAME No. 95
		//	new URI('gfx/duck.gif')
			new URI('gfx/dove2.gif')
		//	new URI('gfx/flower.gif')
		//	new URI('gfx/images/character/10-4.gif')
		//	new URI('gfx/images/character/10-5.gif')
		//	new URI('gfx/images/character/11-4.gif')
		//	new URI('gfx/images/character/11-5.gif')
		//	new URI('gfx/images/character/bonus.png')
		//	new URI('gfx/images/jungle.png')
		//	new URI('gfx/kickboxer.gif')
		//	new URI('gfx/shoe.gif')
		//	new URI('gfx/test_mc.gif')
		//	new URI('http://gifzona.com/i/mult/05.gif')
		//	new URI('http://gifzona.com/i/priroda/1111.gif') //  STRANGE bottom jitting
		//	new URI('http://livegif.ru/Gallery/BEST/fox1.gif')
		//	new URI('http://pimg.dt00.net/pnews/flashki.ru/681770_b.gif')
		//	new URI('http://www.gas13.ru/v3/pixelart/wall.gif')
		//	new URI('http://www.gas13.ru/v3/tutorials/11-5.gif')
		//	new URI('http://www.gas13.ru/v3/tutorials/8-16.gif')
		//	new URI('http://www.gifandgif.eu/animated_gif/Arrows/Animated%20Gif%20Arrows%20(104).gif')
		//	new URI('http://www.livegif.ru/Gallery/ZVERI/BIRDS/BIRDHUML.GIF')
		//	new URI('http://www.pixeljoint.com/files/icons/fool.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/4.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/assault_prototype.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/avat.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/dog.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/food.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/newmario.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/pcdesign_final.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/shoe.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/honey_run_arm00.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/px_spear.gif')
		//	new URI('http://gifdr.narod.ru/i/dr/03.gif')
		//	new URI('http://gifdr.narod.ru/i/dr/10.gif')
		//	new URI('http://www.princeton.edu/~rvdb/JAVA/election2004/TwoMaps.gif')
		//	new URI('http://www.solarviews.com/raw/jup/ganymed3.gif')
		//	new URI('http://www.pixeljoint.com/files/icons/full/completed.gif')
		];
		
		//throw new GenericError(GenericErrorCode.CLASS_CANNOT_BE_INSTANTIATED);

		this._rdStorage = new Array();

		var rm:			ResourceManager = GoodCore.resourceManager;
		rm.fps = super.stage.frameRate;

		var rd:			ResourceDefinition;
		
		var len:		uint = uris.length;
		var uri:		URI;
		var baseURI:	URI = new URI(super.loaderInfo.url);

		for (var i:uint = 0; i < len; i++) {
			uri = URI(uris[i]);

			if ( uri.isRelative() ) {
				uri.makeAbsoluteURI( baseURI );
			}

			rd = new ResourceDefinition(uri, ResourceBehavior.MOVIE_CLIP);
			this._rdStorage.push(rd);

			// Add new entry to resourceManager.
			rm.addResourceEntry(uri);
		}

		//if (getNetworkingStatus() != NetworkingSatus.NONE) {
			this._logger.info('Load image');
			this.loadNext();
		//} else {
		//	this._logger.error('NetworkingSatus set to NONE');
		//}

	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	/**
	 * 
	 * 
	 */
	private function loadNext():void {
		var rd:ResourceDefinition = ResourceDefinition(this._rdStorage[this._index]);

		// Load resource
		var rv:ResourceVault = GoodCore.resourceManager.getResourceVault(rd.signature);

		if ( !rv.ready ) {
			rv.addEventListener(Event.COMPLETE, this.handler_rvComplete);
		} else {
			this.constructCharacter(rv);
		}

	}

	/**
	 * 
	 * @param resourceVault
	 * 
	 */
	private function constructCharacter(resourceVault:ResourceVault):void {
		var rd:	ResourceDefinition = ResourceDefinition(this._rdStorage[this._index]);
		var c:	Class = resourceVault.getResource( rd.qname.toString() );

		var mc:	MovieClip = new c();
		super.addChildAt(mc, 0);
		//mc.opaqueBackground = 0x6A4EE4;
		mc.addEventListener(MouseEvent.CLICK, this.handler_clickMC);
		mc.x = (super.stage.stageWidth - mc.width) >> 1;
		mc.y = (super.stage.stageHeight - mc.height) >> 1;
		mc.addEventListener(Event.ENTER_FRAME, this.handler_mcEnterFrame);
		//mc.gotoAndStop(0);
		//mc.y = 420;
		mc.cacheAsBitmap = true;
		mc.buttonMode = true;
		//mc.scaleX = 1.01;
		//mc.scaleY = 1.01;
		
		this._mc = mc;
		
		var ttlFrameLabel:Label = new Label(super.stage, 170, 10, 'TTL: ' + mc.totalFrames);
		
		//super.addChildAt(new Bitmap(BitmapData(ResourceManager._bitmapDataCollection[0])), 0);

		this._logger.info('Construct character complete %s0x%s1', mc.width, mc.height);
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */	
	private function handler_pressPlay(event:Event):void {
		if (this._mc) {
			this._mc.play();
		}
	}

	/**
	 * @private
	 */	
	private function handler_rvComplete(event:Event):void {
		this._logger.info('Image loaded');
		var resourceVault:ResourceVault = ResourceVault(event.currentTarget);
		this.constructCharacter(resourceVault);
	}

	/**
	 * @private
	 */	
	private function handler_clickMC(event:Event):void {
		var mc:MovieClip = MovieClip(event.currentTarget);

		if (mc.currentFrame == mc.totalFrames) {
			var frame:uint = 1;
		} else {
			frame = mc.currentFrame + 1;
		}

		mc.gotoAndStop(frame);
		
	}

	/**
	 * @private
	 */	
	private function handler_mcEnterFrame(event:Event):void {
		var mc:MovieClip = MovieClip(event.currentTarget);
		this._label.text = mc.currentFrame.toString();
	}

	/**
	 * @private
	 */	
	private function handler_clickMC1(event:Event):void {
		var mc:DisplayObject = DisplayObject(event.currentTarget);
		mc.removeEventListener(event.type, arguments.callee);
		mc.parent.removeChild(mc);
		System.gc();
	}

}
}